﻿using Common.Interfaces;
using Domain.Exceptions;

namespace Domain.ValueObjects
{
    public class PhoneNumber : ValueObject
    {
        public string Value { get; }

        private PhoneNumber(string value) => Value = value;

        public static PhoneNumber Create(string phoneNumber)
        {
            phoneNumber = phoneNumber.Trim();
            if (string.IsNullOrEmpty(phoneNumber))
                throw new InvalidPhoneNumberException("手机号不能为空");

            if (phoneNumber.Length is not 11)
                throw new InvalidPhoneNumberException("当前仅支持国内11位号码");

            return new(phoneNumber);
        }

        protected override IEnumerable<object> GetEqualityComponents()
        {
            yield return Value;
        }

        public override string ToString()
        {
            return Value;
        }

        public static implicit operator string(PhoneNumber phoneNumber) => phoneNumber.Value;
    }
}
